package wqh.controller.json;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import wqh.domain.SystemLog;
import wqh.pojo.MapRet;
import wqh.pojo.xkLog;
import wqh.service.SystemLogService;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by devts on 2016/9/27.
 */
@Controller
@CrossOrigin(origins = "*")
@RequestMapping("/xkLog")
public class SystemLogJSON {

    @Resource
    private SystemLogService systemLogService;

    @ResponseBody
    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    public Object getAll()
    {
        return MapRet.DATA(systemLogService.findAllRecord());
    }

    @ResponseBody
    @RequestMapping(value = "/getSimpleData" ,method = RequestMethod.GET)
    public Object getSimpleData(@RequestParam(value = "draw",required = true) int draw,
                                @RequestParam(value = "start",required = true) int start,
                                @RequestParam(value = "length",required = true) int length)
    {
        PageHelper.startPage(start / length + 1, length);
        List<SystemLog> systemLogList = systemLogService.findAllRecord();
        int count = systemLogService.count();
        @SuppressWarnings("unchecked")
        PageInfo page = new PageInfo(systemLogList);
        return MapRet.DATATABLES(draw, count, new Long(page.getTotal()).intValue(), systemLogList);
    }

    @ResponseBody
    @RequestMapping(value = "/getData" ,method = RequestMethod.POST)
    public Object getData(  @RequestParam(value = "pageSize",required = false,defaultValue = "10") int pageSizeIn,
                            @RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNumIn,
                            @RequestParam(value = "logType",required = false,defaultValue = "0") String log_type,
                            @RequestParam(value = "start",required = false,defaultValue = "0") String start,
                            @RequestParam(value = "end",required = false,defaultValue = "0") String end)
    {
        PageHelper.startPage(pageNumIn,pageSizeIn);
        List<SystemLog> systemLogList = new ArrayList<SystemLog>();
        if(!log_type.equals("0") && start.equals("0") && end.equals("0"))
        {
            systemLogList = systemLogService.findAllByLogType(log_type);
        }
        else if(log_type.equals("0") && !start.equals("0") && end.equals("0"))
        {
            systemLogList = systemLogService.findAllStart(start);
        }
        else if(log_type.equals("0") && start.equals("0") && !end.equals("0"))
        {
            systemLogList = systemLogService.findAllEnd(end);
        }
        else if(log_type.equals("0") && !start.equals("0") && !end.equals("0"))
        {
            systemLogList = systemLogService.findAllStartEnd(start,end);
        }
        else if(!log_type.equals("0") && !start.equals("0") && end.equals("0"))
        {
            systemLogList = systemLogService.findAllLogTypeStart(log_type,start);
        }
        else if(!log_type.equals("0") && start.equals("0") && !end.equals("0"))
        {
            systemLogList = systemLogService.findAllLogTypeEnd(log_type, end);
        }
        else if(!log_type.equals("0") && !start.equals("0") && !end.equals("0"))
        {
            systemLogList = systemLogService.findAllLogTypeStartEnd(log_type, start, end);
        }
        else
        {
            systemLogList = systemLogService.findAllRecord();
        }
        PageInfo pageInfo = new PageInfo<SystemLog>(systemLogList);
        int pageNum = pageInfo.getPageNum();
        int pageSize = pageInfo.getPageSize();
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return MapRet.PAGE_DATA(systemLogList,pageNum,pageSize,total,pages);
    }

    @ResponseBody
    @RequestMapping(value = "/getLogType", method = RequestMethod.GET)
    public Object getLogType()
    {
        return MapRet.DATA(xkLog.getLogType());
    }

}
